// CodeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MLS.h"
#include "CodeDlg.h"
#include "RTFDlg.h"
#include "MLSScanner.h"


// CCodeDlg dialog

IMPLEMENT_DYNAMIC(CCodeDlg, CDialog)
CCodeDlg::CCodeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCodeDlg::IDD, pParent)
{
}

CCodeDlg::~CCodeDlg()
{
}

void CCodeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CODETEXT, CodeText);
}


BEGIN_MESSAGE_MAP(CCodeDlg, CDialog)
	ON_BN_CLICKED(IDC_VIEWRTF, OnBnClickedViewrtf)
	ON_BN_CLICKED(IDC_SAVERTF, OnBnClickedSavertf)
END_MESSAGE_MAP()


// CCodeDlg message handlers

BOOL CCodeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// DS
	CString str = (IsCode)? _T("MLS Code Preview"):_T("Document Preview");
	str += _T(" - ");str += FileName;
	CodeText.StreamString(RichText);
	return TRUE;
}

void CCodeDlg::OnBnClickedViewrtf()
{
	CRTFDlg rtfDlg;
	rtfDlg.RTFCode = RichText;
	rtfDlg.DoModal();
}

void CCodeDlg::OnBnClickedSavertf()
{
	CFileDialog fldlg(FALSE,_T("rtf"),FileName+_T(".rtf"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("Rich Text Format (*.rtf)|*.rtf||"));
	if(fldlg.DoModal()==IDOK)
	{
		CFile file;
		if(file.Open(fldlg.GetPathName(),CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive))
		{
			file.Write((LPCSTR)RichText, RichText.GetLength());
		}
		else AfxMessageBox(_T("Cannot open file for writting"));
	}
}
